package com.nanjia.boot.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.product.model.form.ProductInfoForm;
import com.nanjia.boot.product.model.query.ProductInfoQuery;
import com.nanjia.boot.product.model.vo.ProductInfoVO;
import com.nanjia.boot.product.service.ProductInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 商品信息前端控制层
 *
 * @author 周洋
 * @since 2025-07-04 15:38
 */
@Tag(name = "商品信息接口")
@RestController
@RequestMapping("/api/v1/product-info")
@RequiredArgsConstructor
public class ProductInfoController  {

    private final ProductInfoService productInfoService;

    @Operation(summary = "商品信息分页列表")
    @GetMapping("/page")
    public PageResult<ProductInfoVO> getProductInfoPage(ProductInfoQuery queryParams ) {
        IPage<ProductInfoVO> result = productInfoService.getProductInfoPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增商品信息")
    @PostMapping
   //@PreAuthorize(@ss.hasPerm('product:product-info:add')")
    public Result<Void> saveProductInfo(@RequestBody @Valid ProductInfoForm formData ) {
        boolean result = productInfoService.saveProductInfo(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取商品信息表单数据")
    @GetMapping("/{id}/form")
    public Result<ProductInfoForm> getProductInfoForm(
        @Parameter(description = "商品信息ID") @PathVariable Long id
    ) {
        ProductInfoForm formData = productInfoService.getProductInfoFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改商品信息")
    @PutMapping(value = "/{id}")
   //@PreAuthorize(@ss.hasPerm('product:product-info:edit')")
    public Result<Void> updateProductInfo(
            @Parameter(description = "商品信息ID") @PathVariable Long id,
            @RequestBody @Validated ProductInfoForm formData
    ) {
        boolean result = productInfoService.updateProductInfo(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除商品信息")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteProductInfos(
        @Parameter(description = "商品信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = productInfoService.deleteProductInfos(ids);
        return Result.judge(result);
    }
}
